/*
 * @Author: 熊燕萍
 * @Date: 2021-02-04 11:45:38
 * @LastEditors: 熊燕萍
 * @LastEditTime: 2022-03-09 19:17:41
 * @Description: file content
 * @FilePath: \ys.nbczpt\src\ys\map\polygon.js
 */
import Feature from 'ol/Feature';
import Poly from 'ol/geom/Polygon';
import Style from 'ol/style/Style';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import { fromExtent } from "ol/geom/Polygon";
import { LinearRing, MultiPolygon } from "ol/geom";
// import VectorLayer from "ol/layer/Vector";
// import VectorSource from "ol/source/Vector";

function Polygon(map) {
    this.map = map;
}

/**
 * @description: 添加多边形
 * @param {array} data 多边形坐标,如[[[112.22,21.23],[112.23,21.23],[112.24,24.12],[113.23,24.56]]]
 * @param {object} layer 图层对象
 * @param {object} styleObj 样式对象
 * {
 *  strokeColor:'',多边形边线颜色
 *  strokeWidth:'',多边形边线宽度
 *  fillColor:'' 多边形填充颜色
 * }
 * @return {*}
 */
Polygon.prototype.addPolygon = function (data, layer, styleObj) {
    let polygonLayer = layer;
    //实例化Vector要素，通过矢量图层添加到地图容器中
    let feature = new Feature({
        geometry: new Poly(data)
    });
    feature.setStyle(styleFunction(styleObj));
    let source = polygonLayer.getSource();
    source.addFeature(feature);
    return source.getExtent()
}

/**
 * @description: 添加多边形（geojson转了feature数据格式）
 * @param {*} features
 * @param {*} layer
 * @param {*} styleObj
 * @return {*}
 */
Polygon.prototype.addPolygonByGeojson = function (features, layer, styleObj) {
    layer.setStyle(styleFunction(styleObj));
    let source = layer.getSource();
    source.addFeature(features);
}
let styleFunction = function (styleObj) {
    const style = new Style({
        stroke: new Stroke({
            color: styleObj.strokeColor || '#FFFFFF',
            width: styleObj.strokeWidth || 1
        }),
        fill: new Fill({
            color: styleObj.fillColor || "rgba(255,255,255,0.5)"
        })
    });
    return style;
};
/**
 * @description: 行政区遮罩（带洞多边形）
 * @param {*} layer 遮罩图层
 * @param {*} holeGeom 行政区多边形
 * @param {*} styleObj 遮罩样式
 * @return {*}
 */
Polygon.prototype.addPolygonHoles = function (layer, holeGeom, styleObj = {}) {
    let extent = [-180, -90, 180, 90];
    let polygonRing = fromExtent(extent); //从范围创建多边形
    let coords = holeGeom.getCoordinates(); //获取此几何的坐标数组。该数组具有用于多边形的GeoJSON坐标数组的结构
    if (holeGeom instanceof MultiPolygon)
        coords.forEach(coord => {
            let linearRing = new LinearRing(coord[0]);
            polygonRing.appendLinearRing(linearRing); //将传递的线环添加到这个多边形
        });
    else {
        let linearRing = new LinearRing(coords[0]);
        polygonRing.appendLinearRing(linearRing); //将传递的线环添加到这个多边形
    }
    let feature = new Feature({ geometry: polygonRing });
    feature.setStyle(styleFunction(styleObj));
    layer.getSource().addFeature(feature);
}

Polygon.prototype.addPolygonHolesByCoordinates = function (layer, holeCoordinates, styleObj = {}) {
    let extent = [-180, -90, 180, 90];
    let polygonRing = fromExtent(extent); //从范围创建多边形
    let coords = holeCoordinates; //获取此几何的坐标数组。该数组具有用于多边形的GeoJSON坐标数组的结构
    coords.forEach(coord => {
        let linearRing = new LinearRing(coord[0]);
        polygonRing.appendLinearRing(linearRing); //将传递的线环添加到这个多边形
    });
    let feature = new Feature({ geometry: polygonRing });
    feature.setStyle(styleFunction(styleObj));
    layer.getSource().addFeature(feature);
}
export default Polygon
